package com.qianchang.microfinance.view.mine.loan;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.RepaymentRecordBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.base.EmptyLayout;
import com.qianchang.microfinance.view.base.RecyclerRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.qianchang.microfinance.R.id.error_layout;
import static com.qianchang.microfinance.view.base.EmptyLayout.NO_DATA;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 还款记录
 * 修 改 人: zhoulai
 * 修改时间: 2017/7/5 15:03
 */


public class RepaymentRecordActivity extends BaseActivity {//RepaymentRecordBean


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(error_layout)
    EmptyLayout errorLayout;
    @BindView(R.id.refreshLayout)
    RecyclerRefreshLayout refreshLayout;
    @BindView(R.id.tv_total)
    DTextView tvTotal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repayment_record);
        ButterKnife.bind(this);
        setTitleName(getString(R.string.Label_reply_record));
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        errorLayout.findViewById(R.id.animProgress).setVisibility(View.GONE);
        errorLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        String projectId = getBundle().getString(ConstantAppInfo.PROJECT_ID);
        if (projectId == null) {
            showDataError();
            return;
        }
        HttpParams params = new HttpParams();
        params.put("projectId", projectId);
        show();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_REPAY_RECORD, params, new RequestCallBack<RepaymentRecordBean>() {
            @Override
            public void onSuccess(JsonResult<RepaymentRecordBean> response) {
                dismiss();
                refreshLayout.setRefreshing(false);
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    if (response.getObject() != null) {
                        initList(response.getObject());
                    }
                } else {
                    ToastUtil.getInstant().show(RepaymentRecordActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                refreshLayout.setRefreshing(false);
                ToastUtil.getInstant().show(RepaymentRecordActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });

    }

    private void initList(RepaymentRecordBean bean) {
        if (bean != null && bean.getList() != null) {
            tvTotal.setTextContent(FormatUtil.numKbPointFormat(bean.getRepaidAmount()));
            if (bean.getList().isEmpty()) {
                errorLayout.setErrorType(NO_DATA);
                errorLayout.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            } else {
                errorLayout.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
                RepaymentRecordAdapter adapter = new RepaymentRecordAdapter(this, bean.getList(), R.layout.adapter_repay_record);
                recyclerView.setAdapter(adapter);
            }
        }
    }
}
